<?php
/**
 * Created by PhpStorm.
 * User: CS
 * Date: 2018/11/14
 * Time: 16:35
 */
//连库
require './conn.php';
require  './chuli.php';
//判断产生哪个数据
$type = $_GET['type'];
$id = $_SESSION['id'];

switch ($type){
    case 'lhj'://老虎机数据---------------------------------------------
        //接收用户数据
        $userch = $_POST['userch'];
        if(!is_string($userch)){

        }
        //检测积分是否足够
        $curjifen = getjifen($con,$id);
        if($yaya > $curjifen){
            exit('当前积分不足');
        }
        $yaya = $_POST['yaya'];
        if(!is_numeric($yaya)){
            exit('传入数据异常！');
        }
        //产生数据
        $random = mt_rand(1111,9999);
        //获取后三位
        $arr = str_split((string)$random);
        $one = $arr[1];
        $two = $arr[2];
        $thr = $arr[3];
        // 判断后三位
        $jieguo = [];
        //判断单双
        if ($thr % 2 == 0) {
            $jieguo[] = '双';
        } else {
            $jieguo[] = '单';
        }
        //判断对子
        if ($one == $two || $one == $thr || $two == $thr) {
            //判断豹子
            if ($one == $two && $two == $thr) {
                if($one == $arr[0]){
                    $jieguo[] = '金钱豹';
                }
                $jieguo[] = '豹';
            } else {
                $jieguo[] = '对';
            }
        }
        //判断顺子
        if (max($one, $two, $thr) - min($one, $two, $thr) == 2 && $one != $two && $one != $thr && $two != $thr) {
            if($arr[0] == (min($one, $two, $thr) - 1) || $arr[0] == (max($one, $two, $thr) + 1)){
                $jieguo[] = '连顺';
            }
            $jieguo[] = '顺';
        }

        //判断倍数
        if (in_array($userch, $jieguo)) {
            switch ($userch) {
                case '单':
                    $beishu = 2;
                    break;
                case '双':
                    $beishu = 2;
                    break;
                case '对':
                    $beishu = 4;
                    break;
                case '顺':
                    if(in_array('连顺',$jieguo)){//隐藏关，后面为顺子时 能和第一个也组成顺子
                        $beishu = 12;
                    }else{
                        $beishu = 8;
                    }
                    break;
                case '豹':
                    if(in_array('金钱豹',$jieguo)){//隐藏关，四个同样的数组
                        $beishu = 50;
                    }else{
                        $beishu = 16;
                    }
                    break;
            }
            $msg = '恭喜你！中了'.$beishu.'倍！';
        } else {
            $beishu = 0;
            $msg = '不要灰心，就差一点点啦！';
        }
        $change = $yaya * $beishu - $yaya;
        $new = $curjifen + $change;
        $re = updatejifen($con,$id,$new);
        if($re){
            echo json_encode(['num'=>$random,'jifen'=>$new,'xiaoxi'=>$msg]);
        }else{
            echo 0;
        }
        break;
    case 'sta'://开始查积分---------------------------------------------
        $curjifen = getjifen($con,$id);
        if($curjifen){
            echo json_encode(['jifen'=> $curjifen]);
        }else{
            echo 0;
        }

        break;
    case 'stb'://剪刀石头布--------------------------------------------------------
        //获取用户出的
        //检测积分是否足够
        $curjifen = getjifen($con,$id);
        $yaya = $_POST['yaya'];
        if($yaya > $curjifen){
            exit('当前积分不足');
        }
        if(!is_numeric($yaya)){
            exit('传入数据异常！');
        }
        $userch = $_POST['userch'];
        if(!is_numeric($userch)){
            exit('传入数据异常！');
        }
        $aich = mt_rand(0,2);
        //比较大小
        if ($userch == $aich) {//平手
             $beishu = 1;
             $msg = '嘻嘻嘻！平手！';
        } else if ($userch == 0) {
            if ($aich == 1) {//输了
                $beishu = 0;
                $msg = '嘻嘻嘻！你输了！';
            } else {//赢了
                $beishu = 2;
                $msg = '哟哟哟！你赢了！';
            }
        } else if ($userch - $aich == 1) {//赢了
            $beishu = 2;
            $msg = '哟哟哟！你赢了！';
        } else {//输了
            $beishu = 0;
            $msg = '嘻嘻嘻！你输了！';
        }
        $change = $yaya * $beishu - $yaya;
        $new = $curjifen + $change;
        $re = updatejifen($con,$id,$new);
        if($re){
            echo json_encode(['num'=>$aich,'jifen'=>$new,'xiaoxi'=>$msg]);
        }else{
            echo 0;
        }
        break;
    case 'dzp':
        //检测积分是否足够
        $curjifen = getjifen($con,$id);
        $yaya = $_POST['yaya'];
        if($yaya > $curjifen){
            exit('当前积分不足');
        }
        if(!is_numeric($yaya)){
            exit('传入数据异常！');
        }
        //产生随机角度
        $edg = mt_rand(0,360);
        //panduan
        if ($edg >= 0 && $edg < 45) {
            $beishu = 3;//8
            $msg = '3倍积分！！';
        } else if ($edg >= 90 && $edg < 135 || $edg >= 270 && $edg < 315) {
            $beishu = 1;
            $msg = '1倍积分！';
        } else if ($edg >= 180 && $edg < 225) {
            $beishu = 2;
            $msg = '2倍积分！！';
        } else {
            $beishu = 0;
            $msg = '差一点点啦！';
        }
        $change = $yaya * $beishu - $yaya;
        $new = $curjifen + $change;
        $re = updatejifen($con,$id,$new);
        if($re){
            echo json_encode(['edg'=>$edg,'jifen'=>$new,'xiaoxi'=>$msg]);
        }else{
            echo 0;
        }
        break;
}